<!--
 * @Description: 你的代码我的心
 * @Author: GGB
 * @Date: 2021-12-24 14:08:47
 * @LastEditors: GGB
 * @LastEditTime: 2021-12-30 09:27:25
-->
<template>
  <BasicModal v-bind="$attrs" @ok="handleOk" @register="register" title="修改订单价格">
    <!-- 表单信息 -->
    <BasicForm class="p-4" @register="formRegister" />
  </BasicModal>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { updatePriceSchemas } from './modal.data';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, FormSchema, useForm } from '/@/components/Form';
import { useMessage } from '/@/hooks/web/useMessage';
import { UpdatePriceType } from '/@/api/order/model/orderModel';
import { updatePrice } from '/@/api/order/event';
import { OrderRecordModel } from '../model';
// 弹框的数据记录
const record = ref<OrderRecordModel | any>({})
let reload = () => { };

// 
const [formRegister, { setFieldsValue, validate }] = useForm({
  schemas: updatePriceSchemas,
  layout: 'vertical',
  showActionButtonGroup: false,
  actionColOptions: {
    span: 24
  }
});
//  初始化弹框信息
const callback = (data) => {
  // 初始化表单 
  record.value = data.record
  reload = data.reload;
  setFieldsValue(data.record)
}
const [register, { closeModal, changeOkLoading }] = useModalInner(callback);


// 更改价格
const handleOk = () => {
  validate().then(res => {
    if (!res.errorFields) {
      let param = {
        orderId: record.value.order_id,
        form: res
      } as UpdatePriceType
      changeOkLoading(true)
      const { createMessage } = useMessage();
      updatePrice(param).then(() => {
        createMessage.success("修改成功");
        reload()
        closeModal();
      }).catch(() => {
        createMessage.error("修改失败");
      }).finally(() => {
        changeOkLoading(false)
      })
    }

  })
}
</script>
